package xin.marcher.wind.migrate.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import xin.marcher.wind.migrate.service.MigrateConfigService;
import xin.marcher.wind.migrate.service.MigrateService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 首页controller
 */
@Slf4j
@Controller
@RequestMapping("/")
public class IndexController {

    @Resource
    private MigrateService migrateService;

    @Resource
    MigrateConfigService migrateConfigService;

    /**
     * 跳转到首页
     *
     * @return 首页 html 名称
     */
    @GetMapping("/index")
    public String toIndex(HttpServletRequest request) {
        List<String> scrollAbleTables = migrateService.getScrollAbleTables();
        List<String> domainNames = migrateConfigService.getDomainNames();
        if (CollUtil.isNotEmpty(scrollAbleTables)) {
            request.setAttribute("scrollAbleTables", JSONUtil.toJsonStr(scrollAbleTables));
        }

        if (CollUtil.isNotEmpty(domainNames)) {
            request.setAttribute("domainNames", JSONUtil.toJsonStr(domainNames));
        }
        return "index";
    }
}
